﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using AfterlifeLib;

namespace AfterlifeEditor.Controls
{
    using XRectangle = Microsoft.Xna.Framework.Rectangle;
    using SRectangle = System.Drawing.Rectangle;
    using AfterlifeLib.Levels;

    public class TileSelector : System.Windows.Forms.Panel
    {
        Image tileset;

        Pen gridPen = new Pen(System.Drawing.Color.White, 1f);
        Pen selectedPen = new Pen(System.Drawing.Color.Fuchsia, 2f);

        private SRectangle selected;
        public SRectangle Selected
        {
            get { return selected; }
        }

        public int SelectedIndex
        {
            get
            {
                if (selected == null || selected == SRectangle.Empty)
                    return -1;

                return TileLayer.GetRectangleIndex(tileset.Width, selected);
            }
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            if (tileset != null)
            {
                e.Graphics.DrawImage(tileset, 0, 0, tileset.Width, tileset.Height);

                for (int x = 0; x < tileset.Width; x += Level.TileWidth)
                {
                    e.Graphics.DrawLine(gridPen, new Point(x, 0), new Point(x, tileset.Height - 1));
                }
                for (int y = 0; y < tileset.Height; y += Level.TileHeight)
                {
                    e.Graphics.DrawLine(gridPen, new Point(0, y), new Point(tileset.Width - 1, y));
                }

                if (selected != null)
                {
                    e.Graphics.DrawRectangle(selectedPen, new SRectangle(selected.X, selected.Y, selected.Width, selected.Height));
                }
            }

            base.OnPaint(e);
        }

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            if (tileset == null)
                return;

            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if(e.X < tileset.Width && e.Y < tileset.Height)
                    selected = GetSelectedRectangle(e.Location);

                Invalidate();
            }
        }

        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            OnMouseDown(e);
        }

        public void SetTileSet(Image image)
        {
            if (image == null)
                return;

            tileset = image;

            Invalidate();
        }

        public SRectangle GetSelectedRectangle(Point p)
        {
            if (tileset == null)
                return new SRectangle(0, 0, 0, 0);

            int x = (p.X / Level.TileWidth) * Level.TileWidth;
            int y = (p.Y / Level.TileHeight) * Level.TileHeight;

            return new SRectangle(x, y, Level.TileWidth, Level.TileHeight);
        }
    }
}
